﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Albums;
using VK.Comm.TransactSql;
using VK.Casemeet.Photos;
using System.Web.Script.Serialization;
using VK.Casemeet.Merchants;
using System.Data;

namespace VK.Casemeet.Web.ShopAdmin.Albums
{
    public partial class AlbumEdit : System.Web.UI.Page
    {
        public string imgUrl = "Null.jpg";

        public int albumID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/ShopAdmin/Home.aspx"},
                new MapPath{ Text="相册列表",Url="/ShopAdmin/Albums/AlbumList.aspx"},
                new MapPath{ Text="编辑相册",Url="#"}
            };

            albumID = Util.GetQueryInt("AlbumID");

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        internal void LoadData()
        {
            if (albumID != 0)
            {
                AlbumEntity album = AlbumBLL.GetAlbum(albumID);
                txtName.Text = album.Name;
                txtCreated.Text = Util.FormatDateTime(album.Created, DateTimeStyle.ShortDate2);

                IList<Condition> conditionPhotolst = new List<Condition>();
                conditionPhotolst.Add(new Condition { TableName = "Photo", FieldName = "ObjectType", ConditionType = ConditionType.NumeralEqual, ConditionValue = 3, DbType = DbType.Int32 });
                conditionPhotolst.Add(new Condition { TableName = "Photo", FieldName = "ObjectID", ConditionType = ConditionType.NumeralEqual, ConditionValue = albumID, DbType = DbType.Int32 });
                rptPhoto.DataSource = PhotoBLL.GetPhotoList(conditionPhotolst, "Created", SortMode.Asc);
                rptPhoto.DataBind();
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            AlbumEntity album = LoadEntity();
            album.AlbumID = albumID;
            if (album.ObjectID != AdminBLL.GetSessionAdmin().ShopID) return;

            if (AlbumBLL.UpdateAlbum(album) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected AlbumEntity LoadEntity()
        {
            AlbumEntity album = AlbumBLL.GetAlbum(albumID);
            album.Name = txtName.Text;
            album.Created = Util.ConverDateTime(txtCreated.Text);
            string[] imgUrlArray = new JavaScriptSerializer().Deserialize<string[]>(Util.GetFormString("hidImgUrlArray"));
            album.ImgUrllst = new List<string>(imgUrlArray);

            return album;
        }
    }
}